package com.summer.framework.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.summer.framework.base.exception.SummerRuntimeException;
import com.summer.framework.base.result.SummerPage;
import com.summer.framework.base.lock.LockService;
import com.summer.framework.core.utils.PageUtil;
import com.summer.framework.system.converter.UserInfoConverter;
import com.summer.framework.system.controller.dto.UserInfoDTO;
import com.summer.framework.system.entity.UserInfo;
import com.summer.framework.system.mapper.UserInfoMapper;
import com.summer.framework.system.controller.query.UserInfoQuery;
import com.summer.framework.system.service.UserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.locks.Lock;

/**
 * 用户信息 服务实现类
 * @author Ice
 * @since 2025-09-16
 */
@Service
@RequiredArgsConstructor
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    private final UserInfoConverter converter;

    private final LockService lockService;

    private final static String USER_NAME_LOCK_KEY = "user_name_lock_key";

    @Override
    public SummerPage<UserInfoDTO> page(UserInfoQuery query) {
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(query.getUsername()),UserInfo::getUsername,query.getUsername())
                .like(StringUtils.hasText(query.getName()),UserInfo::getName,query.getName())
                .like(StringUtils.hasText(query.getNickName()),UserInfo::getNickName,query.getNickName())
                .like(StringUtils.hasText(query.getEmail()),UserInfo::getEmail,query.getEmail())
                .like(StringUtils.hasText(query.getPhone()),UserInfo::getPhone,query.getPhone())
                .eq(query.getSex() != null,UserInfo::getSex,query.getSex())
                .eq(query.getAge() != null,UserInfo::getAge,query.getAge())
                .eq(StringUtils.hasText(query.getStatus()),UserInfo::getStatus,query.getStatus())
                .orderByDesc(UserInfo::getCreateTime);
        Page<UserInfo> result = baseMapper.selectPage(query, wrapper);
        return PageUtil.toPage(result,converter::toDTOs);
    }

    @Override
    public void saveUser(UserInfoDTO dto) {
        if(dto == null){
            return;
        }
        Lock lock = lockService.getLock(USER_NAME_LOCK_KEY);
        lock.lock();
        try {
            UserInfo userInfo = baseMapper.selectOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getUsername,dto.getUsername()));
            if(userInfo != null){
                throw new SummerRuntimeException("用户名已存在");
            }
        } finally {
            lock.unlock();
        }
        UserInfo entity = converter.toEntity(dto);
        baseMapper.insert(entity);
    }
}
